﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private string specialChar = "~!@#$%^&*()_+-={}[]|:;\"'<>,.?/";
        private int CheckPass(string pass)
        {
            // 检查长度
            if (pass.Length < 8)
                return 0;

            int[] cType = new int[4]; // 0:数字, 1:大写, 2:小写, 3:特殊字符

            foreach (char c in pass)
            {
                if (Char.IsDigit(c))
                    cType[0] = 1;
                else if (Char.IsUpper(c))
                    cType[1] = 1;
                else if (Char.IsLower(c))
                    cType[2] = 1;
                else if (specialChar.IndexOf(c) >= 0)
                    cType[3] = 1;
                else
                    return 0; // 包含非法字符
            }

            return cType.Sum();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string password = txtPassword.Text;
            int strength = CheckPass(password);

            // 隐藏所有强度标签
            lblWeak.Visible = false;
            lblMedium.Visible = false;
            lblStrong.Visible = false;
            lblVeryStrong.Visible = false;

            switch (strength)
            {
                case 0:
                    MessageBox.Show("密码不得小于8位！\r\n且只能是字母、数字和以下特殊字符的组合：\r\n" + specialChar, "密码不合法", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    break;
                case 1:
                    lblWeak.Visible = true;
                    break;
                case 2:
                    lblMedium.Visible = true;
                    break;
                case 3:
                    lblStrong.Visible = true;
                    break;
                case 4:
                    lblVeryStrong.Visible = true;
                    break;
            }
        }
    }
}
